package com.sservice.web.controller.admin;

import com.sservice.common.Constants;
import com.sservice.common.SameUrlData;
import com.sservice.model.dao.ApplyPayBillMapper;
import com.sservice.urp.dao.IDepDao;
import com.sservice.urp.dao.IUserDao;
import com.sservice.model.vo.ApplyPayBillVo;
import com.sservice.service.service.IApplyPayBillService;
import com.sservice.service.service.ISysLogService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.model.entity.ApplyPayBill;
import com.sservice.model.entity.Clientele;
import com.sservice.urp.entity.Dep;
import com.sservice.urp.entity.User;
import com.sservice.web.log.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by suweiming on 2017/12/20.
 */

@Controller
@RequestMapping("/admin/applyPayBill")
public class ApplyPayBillController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IApplyPayBillService service;
    @Autowired
    private ApplyPayBillMapper mapper;
    @Autowired
    private IDepDao depDao;
    @Autowired
    private ISysLogService sysLogService;
    @Autowired
    private IUserDao userDao;

    /*
     *  查询供应商可用预付款单
     * @param arv
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public BaseResult apply(ApplyPayBill arv, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        User user = (User) request.getSession().getAttribute(Constants.USER);
        arv.setUserName(user.getName() != null ? user.getName() : user.getUserName());
        arv.setApplier(String.valueOf(user.getId()));
        if (user.getId() == 1) {
            return new BaseResult(false, "超级管理员不能申请付款!", 403);
        }
        Object service_res = service.create(arv);
        if (service_res instanceof Integer) {
            int c = (int) service_res;
            if (c > 0) {
                sysLogService.log("申请付款金额：" + arv.getTotal(), request, (Integer) service_res, Action.applyPay);
            }
        }
        BaseResult<Clientele> res = new BaseResult(true, service_res);
        return res;
    }

    /**
     *  付款申请单，出纳核算前有效
     * @param arv
     * @param request
     * @return
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult delete(ApplyPayBill arv, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        arv = mapper.selectByPrimaryKey(arv.getId());
        Object service_res = service.cancel(arv);
        if (service_res instanceof Integer) {
            int c = (int) service_res;
            if (c > 0) {
                sysLogService.log("删除付款申请单：" + arv.getTotal(), request, (Integer) service_res, Action.applyPay);
            }
        }else if(service_res instanceof String){
            return new BaseResult(false, String.valueOf(service_res),403);
        }
        BaseResult<Clientele> res = new BaseResult(true, service_res);
        return res;
    }



    @ResponseBody
    @RequestMapping(value = "/print", method = RequestMethod.GET)
    public BaseResult print(ApplyPayBill arv, HttpServletRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        User user = (User) request.getSession().getAttribute(Constants.USER);
        if (user.getId() == 1) {
            return new BaseResult(true, "超级管理员不能打印付款申请单", 403);
        }
        ApplyPayBill apb = mapper.selectByPrimaryKey(arv.getId());
        Map map = new HashMap();
        map.put("apb", apb);
        User user1 = userDao.selectByPrimaryKey(Integer.valueOf(apb.getApplier())); // 申请用户
        map.put("user", user1);
        // 申请用户所在部门
        Dep dep = depDao.selectByPrimaryKey(Integer.valueOf(user1.getDepid()));
        map.put("dep", dep);
        sysLogService.log("打印", request, arv.getId(), Action.applyPay);
        ListResult<Clientele> res = new ListResult(true, map);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(ApplyPayBillVo arv) {
        return service.page(arv);
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public OutputStream export(ApplyPayBillVo arv, HttpServletRequest request, HttpServletResponse response) {
        try {
            return service.export(arv, request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @ResponseBody
    @RequestMapping(value = "/queryOrder", method = RequestMethod.GET)
    public ListResult queryOrder(ApplyPayBillVo apb) {
        apb.setType("1");
        List<ApplyPayBillVo> list = mapper.queryOrderByPage(apb);
        int total = mapper.queryOrderByCount(apb);
        Pagination page = new Pagination(apb.getPage(), total, apb.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
